package com.info3k.server.domain;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.info3k.client.domain.Event;
import com.info3k.client.domain.Location;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class EventP implements Serializable {

	private static final long serialVersionUID = -1873799364997737422L;

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;
    
    @Persistent
	private String title;
    
    @Persistent
    private Text description;
    
    @Persistent
    private Date creationTime;
    
    @Persistent
    private Date eventTime;
    
    @Persistent
    private Set<Key> locations;
    
    @Persistent
    private Key user;
    
    @Persistent
    private Double rate;
    
    @Persistent
    private Long views;
    
    @Persistent
    private Long numRates;
    
    public Set<Key> getLocations() {
		return locations;
	}

	public void setLocations(Set<Key> locations) {
		this.locations = locations;
	}
    
    public EventP() {
    	
    }
    
    public EventP(Event event) {
    	this.creationTime = event.getCreationTime();
    	this.setDescription(new Text(event.getDescription()));
    	this.eventTime = event.getEventTime();
    	this.title = event.getTitle();
    	
    	DecimalFormat twoDForm = new DecimalFormat("#.##");
    	
    	this.setRate(Double.valueOf(twoDForm.format(event.getRate())));
    	this.setViews(event.getViews());
    	this.setNumRates(event.getNumRates());
    	if(event.getId()!=null)
    		this.setId(KeyFactory.createKey(EventP.class.getSimpleName(), event.getId()));
    	else 
    		this.setId(KeyFactory.createKey(EventP.class.getSimpleName(), System.currentTimeMillis()));
    	this.locations = new HashSet<Key>();
    	if(event.getLocations()!=null){
	    	for (Location loc : event.getLocations()) {
				Key k = KeyFactory.createKey(LocationP.class.getSimpleName(), loc.getId());
				this.locations.add(k);
			}
    	}
    	this.setUser(KeyFactory.createKey(UserP.class.getSimpleName(), event.getUser().getId()));
    }
    
    public static Event createEvent(EventP eventP){
    	Event event = new Event();
    	event.setId(eventP.getId().getId());
    	event.setCreationTime(eventP.getCreationTime());
    	event.setDescription(eventP.getDescription().getValue());
    	event.setEventTime(eventP.getEventTime());
    	event.setNumRates(eventP.getNumRates());
    	event.setRate(eventP.getRate());
    	event.setTitle(eventP.getTitle());
    	event.setViews(eventP.getViews());
    	event.setLocations(new Location[0]);
		return event;
    }

	public void setId(Key id) {
		this.id = id;
	}

	public Key getId() {
		return id;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public void setCreationTime(Date creationTime) {
		this.creationTime = creationTime;
	}

	public Date getCreationTime() {
		return creationTime;
	}

	public void setEventTime(Date eventTime) {
		this.eventTime = eventTime;
	}

	public Date getEventTime() {
		return eventTime;
	}

	public void setUser(Key user) {
		this.user = user;
	}

	public Key getUser() {
		return user;
	}

	public void setRate(Double rate) {
		this.rate = rate;
	}

	public Double getRate() {
		return rate;
	}

	public void setViews(Long views) {
		this.views = views;
	}

	public Long getViews() {
		return views;
	}

	public void setNumRates(Long numRates) {
		this.numRates = numRates;
	}

	public Long getNumRates() {
		return numRates;
	}

	public void setDescription(Text description) {
		this.description = description;
	}

	public Text getDescription() {
		return description;
	}
}
